package com.example.testdemo.demos.web.Tool;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SerialNumberTool {

    /**
     * 生成商品编号 16位
     * @param num
     * @return
     */
    public static String SerialNumber(Integer num){
        String numStr =String.valueOf(num);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
        Date now = new Date();
        String dateTime = sdf.format(now);
        if (numStr.length()==1){
            return dateTime+"000"+num;
        }else  if (numStr.length()==2){
            return dateTime+"00"+num;
        }else  if (numStr.length()==3){
            return dateTime+"0"+num;
        }else  if (numStr.length()==4){
            return dateTime+num;
        }else {
            return dateTime+"0000";
        }
    }

    /**
     * 生成订单id 20-21位
     * @return
     */
    public static String OrderNumber(Integer size,Double price){
        double doubleValue = Double.parseDouble(String.valueOf(price));
        int intValue = (int)  Math.ceil(doubleValue);
        String priceStr = String.valueOf(intValue);
        String numStr =String.valueOf(size);
        String nStr="";
        String pStr="";
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmm");
        Date now = new Date();
        if (numStr.length()==1){
            nStr = "00"+size;
        }else  if (numStr.length()==2){
            nStr = "0"+size;
        }else  if (numStr.length()==3){
            nStr = String.valueOf(size);
        }else {
            nStr = "000";
        }
        String dateTime = sdf.format(now);
        if (priceStr.length()==1){
            pStr = "000000"+priceStr;
        }else  if (priceStr.length()==2){
            pStr = "00000"+priceStr;
        }else  if (priceStr.length()==3){
            pStr = "0000"+priceStr;
        }else if (priceStr.length()==4){
            pStr = "000"+priceStr;
        }else if (priceStr.length()==5){
            pStr = "00"+priceStr;
        }else if (priceStr.length()==6){
            pStr = "0"+priceStr;
        }else if (priceStr.length()==7){
            pStr = "0000"+priceStr;
        }else {
            pStr=priceStr;
        }
        return dateTime+nStr+pStr;
    }

}
